package com.yjck.fm.system.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import com.yjck.fm.common.BasicAction;
import com.yjck.fm.common.Constants;
import com.yjck.fm.util.SystemProperties;

public class CodeGenAction extends BasicAction {

	private String pkgName;
	private String className;
	String[] dir = { "action", "service", "dao", "dao/impl", "dao/handler" };
	public String codeGenerate() throws Exception {
		//创建目录
		createDirectory(pkgName.replace(".", "/"));
		this.map.put("result",Constants.TRUE);
		return SUCCESS;
	
	}

	

	public void setPkgName(String pkgName) {
		this.pkgName = pkgName;
	}

	public String getPkgName() {
		return pkgName;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String getClassName() {
		return className;
	}

	private void createDirectory(String path) throws Exception{
		String outPath = SystemProperties.get("code.path");
		  for (int i = 0; i < dir.length; ++i) {
			  String filePath =outPath+"src/" + path + "/" + dir[i].toLowerCase();
		      File file = new File(filePath);
		      file.mkdirs();
		      InputStream is = null;
		      FileOutputStream  fos = null;
		      if(i<3){
		    	  if(i<2){		    		  
		    		  is =  Thread.currentThread().getContextClassLoader().getResourceAsStream("ClassName"+firstToUpper(dir[i])+".txt");
		    		  fos = new FileOutputStream(new File(filePath+"/"+firstToUpper(className)+firstToUpper(dir[i])+".java"));
		    	  }else{
		    		  is =  Thread.currentThread().getContextClassLoader().getResourceAsStream("ClassName"+dir[i].toUpperCase()+".txt");
		    		  fos = new FileOutputStream(new File(filePath+"/"+firstToUpper(className)+dir[i].toUpperCase()+".java"));
		    	  }
		    	
		      }else{
		    	 String cName = dir[i].split("/")[1];
		    	is = Thread.currentThread().getContextClassLoader().getResourceAsStream("ClassName"+firstToUpper(cName)+".txt");
		    	 if(cName.equals("impl")){
		    		fos = new FileOutputStream(filePath+"/"+firstToUpper(className)+"DAO"+firstToUpper(cName)+".java");
		    	}else{
		    		fos = new FileOutputStream(filePath+"/"+firstToUpper(className)+"RowMapper.java");
		    	} 
		      }
		      int read = 0;
			  StringBuffer src = new StringBuffer();
			  src.append("package "+pkgName+"."+dir[i].replace("/", ".")+";\r\n");
			  if(dir[i].equals("action")){
				  src.append("import " + pkgName + ".service."+firstToUpper(className)+"Service;");
			  }
			  if(dir[i].equals("service") || dir[i].contains("/impl")){
				  src.append("import " + pkgName + ".dao."+firstToUpper(className)+"DAO;");
			  }
			  while((read=is.read())!=-1){
				  src.append((char)read);
			  };
		      String str1 =src.toString().replace("ClassName", firstToUpper(className));
		      String finStr = str1.replace("className", className);
		      String outStr = new String(finStr.getBytes("ISO-8859-1"),"UTF-8");
		      fos.write(outStr.getBytes());
		      fos.flush();
		      fos.close();
		      }
	}
	public String firstToUpper(String str){
		  return str.substring(0, 1).toUpperCase()+str.substring(1);
	  }
}
